package ru.jetsoft.reg112.desktop.control;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.layout.VBox;
import ru.jetsoft.reg112.desktop.data.Param;

public abstract class BaseGroupControl extends ParamControl {

    private final VBox vBox;
    private final List<ControlContainer> childs;

    public BaseGroupControl(Param param) {
        super(param);
        childs = new LinkedList<>();
        vBox = new VBox();
        vBox.setSpacing(5);
        vBox.setPadding(new Insets(2, 2, 2, 2));
    }

    protected abstract void refresh();

    public VBox getVBox() {
        return vBox;
    }

    @Override
    public Node getNode() {
        return vBox;
    }

    @Override
    public String getValue() {
        return null;
    }

    public List<ControlContainer> getChilds() {
        return childs;
    }

    public void setChilds(Collection<ControlContainer> childs) {
        this.childs.clear();
        this.childs.addAll(childs);
        refresh();
    }

    public void addChilds(Collection<ControlContainer> childs) {
        this.childs.addAll(childs);
        refresh();
    }

    public void addChild(ControlContainer child) {
        this.childs.add(child);
        refresh();
    }

    public boolean removeChild(ControlContainer child) {
        Iterator<ControlContainer> it = childs.iterator();
        while(it.hasNext()) {
            if(it.next() == child) {
                it.remove();
                refresh();
                return true;
            }
        }
        refresh();
        return false;
    }
}
